package hlhj.fhp.supreme.activitys

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.app.AlertDialog
import cn.sharesdk.framework.Platform
import cn.sharesdk.framework.PlatformActionListener
import cn.sharesdk.framework.ShareSDK
import cn.sharesdk.wechat.friends.Wechat
import com.google.gson.Gson
import com.hyphenate.chat.EMClient
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import fhp.hlhj.giantfold.utils.LogUtil
import hlhj.fhp.supreme.BaseAty
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.customview.DownloadDialog
import hlhj.fhp.supreme.javabean.*
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.utils.JsonCallBack
import hlhj.fhp.supreme.utils.MyUtils
import hlhj.fhp.supreme.utils.SPHelper
import kotlinx.android.synthetic.main.activity_settings_aty.*

class SettingsAty : BaseAty() {
    private lateinit var versionStr:String
    private var iswx=""
    private var isqq=""
    private var issina=""
    private lateinit var wx: Platform

    override fun getContentId(): Int {
        return R.layout.activity_settings_aty
    }

    override fun initView() {
        version.text=MyUtils.getVersion(this)
        OkGo.post<PhoneBean>(Urls.BaseUrl+Urls.PHONE)
               .params("type","mobile")
               .execute(object :JsonCallBack<PhoneBean>(this){
                   override fun onSuccess(response: Response<PhoneBean>?) {
                       val bean = response?.body()
                       if (bean?.code==1){
                           way.text="电话${bean?.data.mobile}"
                       }
                   }
               })
        initMob()
    }

    private fun initMob() {
        wx = ShareSDK.getPlatform(Wechat.NAME)
        wx.platformActionListener = object : PlatformActionListener {
            override fun onComplete(p0: Platform?, p1: Int, p2: HashMap<String, Any>?) {
                val wxBean = Gson().fromJson(p0?.db?.exportData(), WxBean::class.java)
                OkGo.post<BaseBean>(Urls.BaseUrl+Urls.BIND_THIRED)
                        .params("type",1)
                        .params("openid",wxBean.openid)
                        .params("name",wxBean.nickname)
                        .execute(object :JsonCallBack<BaseBean>(this@SettingsAty){
                            override fun onSuccess(response: Response<BaseBean>?) {
                                val bean = response?.body()
                                if (bean?.code==1){
                                    MyUtils.toast("绑定成功")
                                    initUser()
                                }
                            }
                        })
            }

            override fun onCancel(p0: Platform?, p1: Int) {
            }

            override fun onError(p0: Platform?, p1: Int, p2: Throwable?) {
            }
        }
    }

    override fun onResume() {
        super.onResume()
        initUser()
    }
    private fun initUser() {
        OkGo.post<UserInfoBean>(Urls.BaseUrl + Urls.UserInfo)
                .execute(object : JsonCallBack<UserInfoBean>(this@SettingsAty) {
                    override fun onSuccess(response: Response<UserInfoBean>?) {
                        val bean = response?.body()
                        if (bean?.code == 1) {
                            iswx = bean?.data.is_wx.toString()
                            isqq = bean?.data.is_qq.toString()
                            issina = bean?.data.is_wb.toString()

                            if (iswx.equals("1")) {
                                tvWx.setText("${bean?.data.wx_name}     解绑")
                            }else{
                                tvWx.text="绑定"
                            }
                            if (isqq.equals("1")) {
                                tvQQ.setText("${bean?.data.wx_name}     解绑")
                            }else{
                                tvQQ.text="绑定"

                            }
                            if (issina.equals("1")) {
                                tvSina.setText("${bean?.data.wx_name}     解绑")
                            }else{
                                tvSina.text="绑定"
                            }
                        }
                    }
                })
    }

    override fun initListener() {
        btName.setOnClickListener {
            OkGo.post<VersionBean>(Urls.BaseUrl+Urls.GET_VERSION)
                    .execute(object :JsonCallBack<VersionBean>(this){
                        override fun onSuccess(response: Response<VersionBean>?) {
                            val bean = response?.body()
                            if (bean?.code==1){
                                if (!bean?.data[0].version_code.equals(MyUtils.getVersion(this@SettingsAty))){
                                    AlertDialog.Builder(this@SettingsAty).setTitle("版本更新")
                                            .setMessage(bean.data[0].upgrade_point)
                                            .setPositiveButton("更新"){
                                                dialog, which ->
                                                DownloadDialog(this@SettingsAty,bean.data[0].url,this@SettingsAty).show()
                                            }
                                            .setNegativeButton("取消"){
                                                dialog, which ->
                                                dialog.dismiss()
                                            }
                                            .show()
                                }else{
                                    AlertDialog.Builder(this@SettingsAty).setTitle("更新")
                                            .setMessage("已经是最新的啦")
                                            .setPositiveButton("确定"){
                                                dialog, which ->
                                                dialog.dismiss()
                                            }
                                            .setNegativeButton("取消"){
                                                dialog, which ->
                                                dialog.dismiss()
                                            }.show()
                                }
                            }
                        }
                    })
        }
        btExit.setOnClickListener {
            finish()
        }
        btChangePWD.setOnClickListener {
            startActivity(Intent(this,ChangePwdAty::class.java))
        }
        btLoginOut.setOnClickListener {
            AlertDialog.Builder(this).setTitle("确认退出？")
                    .setPositiveButton("确定"){
                        dialog, which ->
                        dialog.dismiss()
                        EMClient.getInstance().logout(true)
                        SPHelper.saveLoginStatus(this,false)
                        MyUtils.AtyContainer.getInstance().finishAllActivity()
                        startActivity(Intent(this,LoginAty::class.java))

                    }
                    .setNegativeButton("取消"){
                        dialog, which ->
                        dialog.dismiss()
                    }
                    .show()

        }

        btWx.setOnClickListener {
            if (iswx.equals("1")){
                val intent = Intent(this, UnBindAty::class.java)
                intent.putExtra("type","1")
                startActivity(intent)
            }else{
                wx.authorize()
            }
        }
        btQQ.setOnClickListener {
            if (isqq.equals("1")){
                val intent = Intent(this, UnBindAty::class.java)
                intent.putExtra("type","2")
                startActivity(intent)
            }else{

            }
        }
        btSina.setOnClickListener {
            if (issina.equals("1")){
                val intent = Intent(this, UnBindAty::class.java)
                intent.putExtra("type","3")
                startActivity(intent)
            }else{

            }
        }
    }
}
